package com.wobangkj.common.repository;

import com.wobangkj.common.model.crud.Share;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface ShareRepository extends JpaRepository<Share, Long> {

    @Query(nativeQuery = true, value="select * from  share  where share_title like CONCAT('%',:share_title,'%') ")
    List<Share> findByNameLike(String share_title);

    @Query(nativeQuery = true,value = "select s.*,u.major,u.name,u.mst_company_id,u.mst_job,u.grade,u.school,u.college from share s inner join user u on s.id = u.id group by s.id;") //原生SQL方法
    List<Share> selectAllShareView();

    @Query(nativeQuery = true,value = "select e.*,cp.company_num,cp.business,cp.company_finance,cp.company_logo,cp.company_text,cp.company_name from (select s.*,u.major,u.name,u.mst_company_id,u.mst_job,u.grade,u.school,u.college,u.headimg from share s inner join user u on s.share_id = u.id ) e inner join company cp on e.mst_company_id = cp.id group by e.id;") //原生SQL方法
    List<Map> list();

    @Query(nativeQuery = true,value = "select e.*,cp.company_num,cp.business,cp.company_finance,cp.company_logo,cp.company_text,cp.company_name from (select s.*,u.major,u.name,u.mst_company_id,u.mst_job,u.grade,u.school,u.college,u.headimg from share s inner join user u on s.share_id = u.id ) e inner join company cp on e.mst_company_id = cp.id and e.share_class = :share_class group by e.id;") //原生SQL方法
    List<Map> list(Integer share_class);

    @Query(nativeQuery = true,value = "select e.*,cp.company_num,cp.business,cp.company_finance,cp.company_logo,cp.company_text,cp.company_name from (select s.*,u.major,u.name,u.mst_company_id,u.mst_job,u.grade,u.school,u.college,u.headimg from share s inner join user u on s.share_id = u.id ) e inner join company cp on e.mst_company_id = cp.id and e.id = :id group by e.id;") //原生SQL方法
    List<Map> idlist(Long id);

    @Query(nativeQuery = true,value = "select e.*,cp.company_num,cp.business,cp.company_finance,cp.company_logo,cp.company_text,cp.company_name from (select s.*,u.major,u.name,u.mst_company_id,u.mst_job,u.grade,u.school,u.college,u.headimg from share s inner join user u on s.share_id = u.id ) e inner join company cp on e.mst_company_id = cp.id " +
            "where e.share_company like CONCAT('%',:share_company,'%') and e.college=:college and e.major=:major and e.share_job like CONCAT('%',:share_job,'%');") //原生SQL方法
    List<Map> list(String share_company,Integer college,Integer major,String share_job);

    @Query(nativeQuery = true, value="update share set share_forward_num=share_forward_num+1 where id=:id  ")
    @Modifying
    public void updateShareForward(Long id);

    @Query(nativeQuery = true, value="update share set share_url=:url where id=:id  ")
    @Modifying
    public void updateUrl(String url,String id);

}
